 // pages/list/more-selected/more-selected.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: {},
    contents: '',
    defult: 10,
    isPage: 1,
    requestLoading: false,
    requestLoadingComplete: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
    wx.showNavigationBarLoading();
    var parameter = {
      cid: "",
      num: this.data.defult,
      page: this.data.isPage,
      isChoice: 1
    };
    var getApinavUrl = app.globalData.smsbaoApi + "/batch/sapi/get_sms_cate";
    var getApiContUrl = app.globalData.smsbaoApi + "/batch/sapi/get_sms_content";
    this.getApiData(getApiContUrl, parameter, "contents")
    this.getApiData(getApinavUrl, '', "nav")
  },

  //封装请求地址函数
  getApiData: function (url, parameter, settedKey) {
    let that = this
    wx.request({
      url: url,
      data: parameter,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if(res.data.code == '200'){
          var arrData = {};
          arrData[settedKey] = res.data.data;
          that.setData(arrData);
        }
        wx.hideNavigationBarLoading();
      },
      fail: function (error) {
        console.log(error)
      }
    })
  },
   //跳转短信发送页面
  onBatch: function (event) {
    var data = event.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/batch/batch?data=' + JSON.stringify(data) + "&class=" + 0,
    })
  },
  onNavTo: function (event) {
    var nav = event.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/list/list?nav=" + JSON.stringify(nav),
    })
  },
  //上拉加载
  onReachBottom: function (event) {
    var getApiContUrl = app.globalData.smsbaoApi + "/batch/sapi/get_sms_content";
    var isPage = this.data.isPage++;
    var parameter = {
      cid: '',
      num: this.data.defult,
      page: isPage,
      isChoice: 1
    };
    this.getApiContent(getApiContUrl, parameter)
    wx.showNavigationBarLoading();
  }, 
  getApiContent: function (url, parameter) {
    let that = this;
    wx.request({
      url: url,
      data: parameter,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == '200') {
          if (!that.data.isEmpty) {
            if (res.data.code == '200' && res.data.data == undefined || res.data.data == '') {
              that.setData({
                requestLoadingComplete: false,
                requestLoading: true
              })
              wx.hideNavigationBarLoading();
              wx.stopPullDownRefresh();
              return false;
            } else {
              var updata = res.data.data;
              var data = that.data.contents.concat(updata);
              that.setData({
                contents: data
              })
            }
          } else {
            var data = res.data.data;
            that.data.isEmpty = false
            that.setData({
              contents: data
            })
          }

          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh();
        } else {
          return false;
        }
      },
      fail: function (error) {
        console.log(error)
      }
    })

  },
})